home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-api-22.lha / AmiTCP-2.2 / src / netlib / getgroupent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-22  |  3.6 KB  |  191 lines

  1. RCS_ID_C "$Id: getgroupent.c,v 1.3 1993/10/15 01:16:22 ppessi Exp $";
  2. /*
  3.  * getgroupent.c - getgrp*() for network library
  4.  *
  5.  * This file is part of the AmiTCP/IP Network Library.
  6.  *
  7.  * Author: ppessi <Pekka.Pessi@hut.fi>
  8.  * Copyright © 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  9.  *                  Helsinki University of Technology, Finland.
  10.  *
  11.  * Created      : Sun Jun 20 17:52:37 1993 ppessi
  12.  * Last modified: Thu Oct 14 19:25:25 1993 ppessi
  13.  */
  14.  
  15. #include <exec/memory.h>
  16. #include <dos/dos.h>
  17.  
  18. #include <string.h>
  19. #include <sys/errno.h>
  20.  
  21. #include "grp.h"
  22.  
  23. #if !NETLIB
  24. #error 
  25. #else
  26. #include <proto/dos.h>
  27.  
  28. #include <errno.h>
  29. #define SetNetErr(x) (errno=(x))
  30. static BPTR grpfh = NULL;
  31.  
  32. #define INTERNAL static
  33.  
  34. #endif
  35.  
  36. /*
  37.  * Parsing function
  38.  */
  39. extern int do_parse(UBYTE *str, int n, UBYTE **array);
  40.  
  41. /* End reading group entities */
  42. INTERNAL void EndGroupEnt(void)
  43. {
  44.   if (grpfh) Close(grpfh), grpfh = NULL;
  45. }
  46.  
  47. /* Restart reading group entities */
  48. INTERNAL void SetGroupEnt(void)
  49. {
  50.   if (grpfh) EndGroupEnt();
  51.   
  52.   grpfh = Open(_PATH_GROUP, MODE_OLDFILE);
  53. #if TESTING
  54.   atexit(EndGroupEnt);
  55. #endif
  56. }
  57.  
  58. /* Read the next entry from the group-file database */
  59. INTERNAL struct group *GetGroupEnt(struct group *g, ULONG size)
  60. {
  61.   UBYTE *p, *buffer = (UBYTE*)g;
  62.   UBYTE **members;
  63.   ULONG s;
  64.   int err = 0;
  65.  
  66.   if (size - sizeof(struct group) < 0) {
  67.     err = ENOBUFS;
  68.     goto bad;
  69.   }
  70.  
  71.   memset(buffer, sizeof(struct group), 0);
  72.  
  73.   size   -= sizeof(struct group) + 1; 
  74.   buffer += sizeof(struct group);
  75.   /* fix a bug in the FGetS routine */
  76.   buffer[size] = '\0';
  77.  
  78.   if (!grpfh) 
  79.     SetGroupEnt();
  80.  
  81.   if (!grpfh || !FGets(grpfh, buffer, size)) {
  82.     err = ENOENT;
  83.     goto bad;
  84.   }
  85.  
  86.   /* Make space for the member list */
  87.   for (s = 2, p = buffer; *p; p++) {
  88.     if (*p == ',') 
  89.       s++;
  90.   }
  91.   if (*--p != '\n' || p - buffer + s * sizeof(*members) > size) {
  92.     /* Too long! */
  93.     err = ENOBUFS;
  94.     goto bad;
  95.   }
  96.  
  97.   {
  98.     UBYTE *from = p + 1, *to = p + s * sizeof(*members) + 1;
  99.     while (from >= buffer) {
  100.       *to-- = *from--;
  101.     }
  102.     buffer = to + 1;
  103.   }
  104.  
  105.   if (err = do_parse(buffer, 4, (UBYTE **)g))
  106.     goto bad;
  107.  
  108.   /* Parse the group ID field */
  109.   if (StrToLong((UBYTE*)g->gr_gid, &g->gr_gid) <= 0)
  110.   if ((s = StrToLong(p, &g->gr_gid)) <= 0 || p[s] != '|') {
  111.     err = EFTYPE;
  112.     goto bad;
  113.   }
  114.  
  115.   /* Parse the memberlist */
  116.   p = (UBYTE*)g->gr_mem;
  117.   for (g->gr_mem = members = (UBYTE **)(g + 1); *p;) {
  118.     *members++ = p;
  119.     while (*p && *p++ != ',')
  120.       ;
  121.   }
  122.   *members = NULL;
  123.  
  124.   return g;
  125.  
  126.  bad:
  127.   SetNetErr(err);
  128.   EndGroupEnt();
  129.   return (struct group*) NULL;
  130. }
  131.  
  132. /* Search for an entry with a matching group ID */
  133. INTERNAL struct group *GetGroupGID(ULONG gid, 
  134.               struct group *g, 
  135.               ULONG size)
  136. {
  137.   while ((g = GetGroupEnt(g, size)) && (g->gr_gid != gid))
  138.     ;
  139.   EndGroupEnt();
  140.   return g;
  141. }
  142.  
  143. /* Search for an entry with a matching group name */
  144. INTERNAL struct group *GetGroupName(const UBYTE *name, 
  145.                struct group *g, 
  146.                ULONG size)
  147. {
  148.   while ((g = GetGroupEnt(g, size)) && strcmp(g->gr_name, name))
  149.     ;
  150.   EndGroupEnt();
  151.   return g;
  152. }
  153.  
  154. /*
  155.  * A global structure for Unix compatible functions
  156.  */
  157. #define GSTORE_SIZE 1024
  158. static struct group * gstore = NULL;
  159.  
  160. #include <stdlib.h>
  161.  
  162. static struct group * alloc_gstore(void)
  163. {
  164.   return gstore = (struct group *)malloc(GSTORE_SIZE);
  165. }
  166.  
  167. /* 
  168.  * POSIX compatible getgrgid()
  169.  */
  170. struct group *getgrgid(gid_t gid)
  171. {
  172.   if (!gstore && !alloc_gstore()) {
  173.     return NULL;
  174.   }
  175.  
  176.   return GetGroupGID((ULONG) gid, gstore, GSTORE_SIZE);
  177. }
  178.  
  179. /*
  180.  * POSIX compatible getgrnam
  181.  */
  182. struct group *getgrnam(const char *name)
  183. {
  184.   if (!gstore && !alloc_gstore()) {
  185.     return NULL;
  186.   }
  187.  
  188.   return GetGroupName((UBYTE *) name, gstore, GSTORE_SIZE);
  189. }
  190.  
  191.